/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename ZActivityIndicatorDialog.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.novell.zenworks.agent.zicon.progress;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.io.File;
import java.text.BreakIterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import com.novell.zenworks.agent.common.Paths;
import com.novell.zenworks.agent.zicon.ZTrayIcon;
import com.novell.zenworks.zicon.common.progress.ITaskSetProgress.ProgressStatus;


/**
 * The application's main frame.
 */
public class ZActivityIndicatorDialog extends JFrame 
{
    private JLabel iconLabel;
    private JLabel activity;
    private JLabel name;
    private JPanel upperPanel;
    private JPanel lowerPanel;
    private JProgressBar progressBar;
    private JPanel activityPanel;

    private static final long serialVersionUID = 1L;
    private final Font font = new Font("Trebuchet MS", Font.PLAIN, 12);
    private final Font bfont = new Font("Trebuchet MS", Font.BOLD, 12);
    
    public ZActivityIndicatorDialog(String title, String action, String entityName) throws Exception
    {
        if(!System.getProperty("os.name").toLowerCase().contains("mac"))
        {
            String titleImage =  ZTrayIcon.getImagesPath() + File.separator + "Application.png";
            ImageIcon titleIcon = new ImageIcon(titleImage);
            this.setIconImage(titleIcon.getImage());
        }
        this.setTitle(title);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        
        initComponents(action, entityName);
        
        this.setSize(350, 160);
        this.setResizable(false);
        this.setVisible(false);
    }

    private void initComponents(String action, String entityName) {
        
        GridBagConstraints gridBagConstraints;

        activityPanel = new JPanel();
        upperPanel = new JPanel();
        iconLabel = new JLabel();
        activity = new JLabel();
        lowerPanel = new JPanel();
        name = new JLabel();
        progressBar = new JProgressBar();

        activityPanel.setName("activityPanel");
        activityPanel.setPreferredSize(new Dimension(350, 160));
        activityPanel.setLayout(new GridBagLayout());

        upperPanel.setName("upperPanel");
        upperPanel.setLayout(new GridBagLayout());


        iconLabel.setText("");
        Icon icon = new ImageIcon(Paths.RESOURCE_DIR + File.separator + "images" + File.separator + "zicon" + File.separator + "nal_icon.png");
        iconLabel.setIcon(icon);
        iconLabel.setMaximumSize(new Dimension(34, 32));
        iconLabel.setMinimumSize(new Dimension(34, 32));
        iconLabel.setName("iconLabel");
        iconLabel.setOpaque(true);
        iconLabel.setPreferredSize(new Dimension(34, 32));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 14;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new Insets(5, 10, 0, 10);
        upperPanel.add(iconLabel, gridBagConstraints);

        activity.setFont(font);
        activity.setText(action); // NOI18N
        activity.setName("activity");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new Insets(0, 10, 0, 10);
        upperPanel.add(activity, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        activityPanel.add(upperPanel, gridBagConstraints);

        lowerPanel.setName("lowerPanel");
        lowerPanel.setLayout(new GridBagLayout());

        name.setFont(bfont);
        wrapMessageText(name, entityName, 300);
        name.setMaximumSize(new Dimension(34, 32));
        name.setMinimumSize(new Dimension(34, 32));
        name.setName("name");
        name.setOpaque(true);
        name.setPreferredSize(new Dimension(34, 32));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new Insets(0, 10, 5, 10);
        lowerPanel.add(name, gridBagConstraints);

        progressBar.setName("progressBar");
        progressBar.setIndeterminate(true);
        progressBar.setForeground(Color.BLUE);
        progressBar.setMaximumSize(new Dimension(1200, 12));
        progressBar.setMaximumSize(new Dimension(200, 12));
        progressBar.setPreferredSize(new Dimension(300, 12));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new Insets(5, 10, 10, 10);
        lowerPanel.add(progressBar, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 10;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        activityPanel.add(lowerPanel, gridBagConstraints);

        add(activityPanel);
    }
    
    public void updateActivityName(String activityName)
    {
        name.setText(activityName);
        validate();
    }
    
    public void updateActivityAction(String action)
    {
        activity.setText(action);
        validate();
    }
    
    public void updateTitle(String title)
    {
        this.setTitle(title);
    }
    
    /**
     * @method Method to auto wrap message text.
     * 
     * @param label message to wrap text for.
     * @param width width for wrap text.
     */
    private void wrapMessageText(JLabel label, String message, int width)
    {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        BreakIterator wall = BreakIterator.getWordInstance();
        wall.setText(message);

        StringBuffer poker = new StringBuffer();
        StringBuffer result = new StringBuffer("<html>");

        int start = wall.first();
        for (int end = wall.next(); end != BreakIterator.DONE; start = end, end = wall.next())
        {
            String word = message.substring(start, end);
            poker.append(word);
            int trialWidth = SwingUtilities.computeStringWidth(fm, poker.toString());
            if (trialWidth > width)
            {
                poker = new StringBuffer(word);
                result.append("<br>");
            }
            result.append(word);
        }

        result.append("</html>");
        label.setText(result.toString());
    }

}


